import config from '@/config.js'

import {
	md5,
	formatterSign,
	queryType
} from './index.js'

import store from '@/store'

//请求防抖
let req_cache_timer = {}
let loading_timer = null


/**
 * 
 * @param {*} api   请求地址
 * @param {*} data  携带数据
 * @param {*} options  请求参数  
 * authToken  1 临时token  2 无token 
 * loading    数据加载
 * throttle   请求防抖
 * @returns 
 */
async function request(api, data, options = {}) {



	return new Promise((resolve, reject) => {

		let obj = {
			url: config.apiUrl,
			method: options.method || 'POST',
			dataType: 'json', //自动JSON.parse
			data: data || {},
			header: options.header || {},
		}

		if (api) {
			if (typeof api != 'string') {
				return Promise.reject('API参数非法！' + JSON.stringify(api))
			}
			obj.url += api
		} else {
			return Promise.reject('API参数非法！')
		}

		console.log('请求接口 / ' + obj.url)
		// console.log('请求参数 / ', obj.data)
		// console.log('——————————————————')


		obj.success = (ret) => {
			
			ret = ret.data
			console.log('接口响应 / ' + obj.url)
			console.log('响应数据 request / ', ret,ret.code != 200 && ret.code != -1)
			// console.log('——————————————————')

			// if (ret.code != 200) {
			// 	if (ret.errorCode == 2 || ret.errorCode == 3 || ret.errorCode == 8) {
			// 		// afreshLogin('登录信息过期 请重新登录！')
			// 		return reject(ret)
			// 	}
			// 	uni.showModal({
			// 		title: '错误',
			// 		content: ret.errorMsg || ret.msg || ret.message,
			// 		showCancel: false
			// 	})

			// 	return reject(ret)
			// }
			return resolve(ret)
		}
		obj.fail = (err) => {
			return reject(err)
		}
		obj.complete = () => {
			if (loading_timer) {
				clearTimeout(loading_timer)
			}
			if (options.loading) {
				loading_timer = setTimeout(() => {
					uni.hideLoading()
					loading_timer = null
				}, 200)
			}
		}

		if (loading_timer) {
			clearTimeout(loading_timer)
			loading_timer = null
		}

		if (options.loading) {
			loading_timer = setTimeout(() => {
				uni.showLoading({
					title: '数据加载中...',
					mask: true
				})
				loading_timer = null
			}, 500)
		}
		//添加防抖
		if (options.throttle) {
			if (req_cache_timer[api]) {
				//中止请求
				req_cache_timer[api].taskId.abort()
				//清理防抖定时器
				clearTimeout(req_cache_timer[api])
				req_cache_timer[api] = null
			}
			req_cache_timer[api] = {}
			req_cache_timer[api].timer = setTimeout(() => {
				req_cache_timer[api].taskId = uni.request(obj)
				clearTimeout(req_cache_timer[api])
				req_cache_timer[api] = null
			}, 300)

		} else {
			uni.request(obj)
		}
	})
}


/**
 * 
 * @param {*} url   文件地址
 * @returns 
 */
export async function upFile(path, file,data ={}) {
	console.log('upFile formData',data);
	let obj = {
		url: config.apiUrl + path,
		name: 'image',
		formData: {
			...data
		},
	}
	// 区分单文件和多文件  
	if (queryType(file) == 'Array') {
		obj.files = file
	} else {
		obj.filePath = file
	}

	return new Promise((resolve, reject) => {
		obj.success = (ret) => {
			if (ret.statusCode != 200) {
				return reject(ret)
			}
			ret = JSON.parse(ret.data)
			console.log('响应数据 upFile / ', ret)
			// if (ret.code != 200 && ret.code != -1) {
			// 	return reject(ret)
			// }
			return resolve(ret)
		}
		obj.fail = (err) => {
			return reject(err)
		}
		obj.complete = () => {

		}
		uni.uploadFile(obj)
	})
}

export default request